package com.br.fmn.clubedoimovel.view;

import java.io.IOException;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.webkit.GeolocationPermissions;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.AutoCompleteTextView;

import com.br.fmn.clubedoimovel.R;
import com.br.fmn.clubedoimovel.negocio.entidade.Coordinates;
import com.br.fmn.clubedoimovel.negocio.map.MyLocation;
import com.br.fmn.clubedoimovel.negocio.model.ApplicationMapModel;
import com.br.fmn.clubedoimovel.negocio.servico.AutoCompleteAsyncTask;
import com.br.fmn.clubedoimovel.negocio.servico.GeocodeAsyncTask;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class ApplicationMap extends MapActivity implements TextWatcher,
		OnItemClickListener {

	private MapView mapView;
	private MyLocation currentMyPosition;
	private ApplicationMapModel applicationModel;
	private LocationManager locationManager;
	private MapController mapController;
	private AutoCompleteTextView autoComplete;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.applicatioin_map_view);

		loadSettingsDefaultMap();
		applicationModel.enableMyLocation();

		//autoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
		//autoComplete.addTextChangedListener(this);
		//autoComplete.setOnItemClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		currentMyPosition = new MyLocation(mapView, getApplication(),
				applicationModel);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
				0, currentMyPosition);

	}

	private void loadSettingsDefaultMap() {

		// cria o mapa.
		mapView = (MapView) findViewById(R.id.mapview);

		// habilita botao de zoom
		mapView.setBuiltInZoomControls(true);

		// seta para padr�o de sat�lite
		mapView.setSatellite(true);

		// instancia o applicationMap
		applicationModel = new ApplicationMapModel(getApplicationContext(),
				mapView);

		// instancia o regenciador de localiza��o
		locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

		// cria o controlador do mapa
		mapController = mapView.getController();

		// inicia o mapa na localiza��o padr�o
		mapController.animateTo(new GeoPoint((int) (-8.599011 * 1E6),(int) (-52.834939 * 1E6)));

		// seta um zoom padr�o.
		mapController.setZoom(5);

	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub

	}

	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {

		AutoCompleteAsyncTask autoCompleteAsync = new AutoCompleteAsyncTask(
				getApplicationContext(), mapView, autoComplete);
		if (start >= 4) {

			autoCompleteAsync.execute(new String[] { s.toString() });
		}
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {

		AutoCompleteAsyncTask autoCompleteAsync = new AutoCompleteAsyncTask(
				getApplicationContext(), mapView, autoComplete);
		if (start >= 4) {

			autoCompleteAsync.execute(new String[] { s.toString() });
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

		new GeocodeAsyncTask().execute(parent.getItemAtPosition(position)
				.toString());

		Location currentLocation = applicationModel.retrieveLastKnownLocation();

		try {

			// Get from address.
			Address address = applicationModel.getGeocode(parent
					.getItemAtPosition(position).toString());
			
			applicationModel.setPinMap(address.getLatitude(),
					address.getLongitude(), R.drawable.pin_origem);

			// Set coordinates
			applicationModel.setCoordinates(new Coordinates(currentLocation
					.getLatitude(), currentLocation.getLongitude(), address
					.getLatitude(), address.getLongitude()));

		} catch (IOException e) {

			e.printStackTrace();
		}

	}
}